package com.inventory.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.inventory.persistence.Address;
import com.inventory.persistence.Country;
import com.inventory.persistence.CountryExample;
import com.inventory.persistence.State;
import com.inventory.persistence.StateExample;
import com.inventory.persistence.dao.AddressMapper;
import com.inventory.persistence.dao.CountryMapper;
import com.inventory.persistence.dao.StateMapper;

public class AddressServiceImpl implements AddressService {
	@Autowired
	private AddressMapper addressMapper;
	@Autowired
	private StateMapper stateMapper;
	@Autowired
	private CountryMapper countryMapper;

	public int createAddress(Address address) {
		address.setVersionStamp(1);
		return addressMapper.insert(address);
	}

	public int updateAddress(Address address) {
		address.setVersionStamp(address.getVersionStamp() + 1);
		return addressMapper.updateByPrimaryKey(address);
	}

	public int deleteAddress(Address address) {
		return addressMapper.deleteByPrimaryKey(address.getOid());
	}

	public List<Country> findAllCountries() {
		CountryExample countryExample = new CountryExample();
		countryExample.createCriteria().andOidGreaterThan((long) 0);
		return countryMapper.selectByExample(countryExample);
	}

	public List<State> getStatesByCountry(Country country) {
		StateExample example = new StateExample();
		example.createCriteria().andCountryOidEqualTo(country.getOid());
		return stateMapper.selectByExample(example);
	}

	public void setAddressMapper(AddressMapper addressMapper) {
		this.addressMapper = addressMapper;
	}

	public void setStateMapper(StateMapper stateMapper) {
		this.stateMapper = stateMapper;
	}

	public void setCountryMapper(CountryMapper countryMapper) {
		this.countryMapper = countryMapper;
	}

	public Country findCountryByOid(Long countryOid) {
		return countryMapper.selectByPrimaryKey(countryOid);
	}

	public State findStateByOid(Long stateOid) {
		return stateMapper.selectByPrimaryKey(stateOid);
	}

}
